<template>
	<view class="uiwu">
		<view class="uiwu-list">
			<view class="uiwu-list-item uiwu-flex-space" v-for="(item, index) in listData" :key="index"
				@tap="openWindow(item)">
				<text class="name">{{ item.title }}</text>
				<uiwu-icon type="arrowright"></uiwu-icon>
			</view>
		</view>

		<!-- #ifdef MP-TOUTIAO -->
		<button open-type="im" :data-im-id="app.douyin_kefu_id" class="btn">
			在线客服
		</button>
		<!-- #endif -->

		<!-- #ifndef MP-TOUTIAO -->
		<view class="btn" @tap.stop="openWidnow">
			在线客服
		</view>
		<!-- #endif -->
	</view>
</template>

<script lang="ts" setup>
import { onLoad, onReachBottom, onShow } from '@dcloudio/uni-app'
import { ref, inject, toRefs, getCurrentInstance } from "vue";
import { problemlist, uiwu } from '@/api/interface/interface';
import { configStore } from '@/store/configStore';
const listData = ref<Array<object>>([])
const pageNum = ref<number>(1)
const more = ref<string>('loading');
const isLock = ref<boolean>(true);

const { app } = toRefs(configStore())
const webwx = ref<any>(null);
const openWindow = (item) => {
	uiwu.openWindow(`/pages/index/problemdetails?id=${item.id}`)
}
const getproblemlist = async (mack: boolean = false): Promise<any> => {
	if (!mack) pageNum.value = 1
	const { data: { data, total } } = await problemlist({
		page: pageNum.value,
	})
	isLock.value = true
	uiwu.setlist({
		isMore: mack,
		listData,
		total,
		listRow: data,
		more,
		pageNum
	})
}
onReachBottom(() => {
	if (!isLock.value) return
	isLock.value = false
	pageNum.value++
	getproblemlist(true)
})

const openWidnow = (item: object): void => {
	// 跳转企业微信客服
	
	// #ifdef APP-PLUS
	uni.showModal({
		title: "温馨提示",
		content: "您即将跳转到外部应用，点击确认继续",
		success(result) {
			if (result.confirm) {
				plus.share.getServices((res) => {
					const wechat = res.find(i => i.id == 'weixin');
					if (!wechat) uiwu.toast('当前环境不支持微信操作')
					wechat.openCustomerServiceChat({
						corpid: app.value.corpId,
						url: app.value.workweixin_kefu_url
					})
				})
			}
		}, fail(error) {
			console.log(error, "error");
		}
	})
	// #endif
	// #ifdef MP-WEIXIN
	wx.openCustomerServiceChat({
		extInfo: { url: app.value.workweixin_kefu_url },
		corpId: app.value.corpId,
		showMessageCard: true,
		sendMessageTitle: '',
		success(res) { },
		fail: err => {
			console.log(err)
		}
	})
	// #endif

	// #ifdef WEB
	console.log(webwx.value.$wx, "wx");
	// console.log(app.value.workweixin_kefu_url)
	window.location.href = app.value.workweixin_kefu_url
	// webwx.value.$wx.openEnterpriseChat({
	// 	// ww90e13ae096ee9b31
	// 	externalUserIds: " ww935ef28fe1584b72",
	// 	groupName: "",
	// 	success(res) {
	// 		console.log(res);
	// 	},
	// 	fail: err => {
	// 		console.log(err, "errors")
	// 	}
	// })
	// #endif
}

onLoad(() => {
	getproblemlist();
})
onShow(() => {
	const instance = getCurrentInstance();
	console.log(instance?.appContext.config.globalProperties, "onShow");
	webwx.value = instance?.appContext.config.globalProperties;
});
</script>

<style lang="scss">
page {
	background-color: #F6F6F6;
}

.uiwu {
	padding: 20rpx 0;
}

.btn {
	background-color: $qj-red;
	border-radius: 60rpx;
	font-size: 30rpx;
	color: #FFF;
	height: 80rpx;
	line-height: 80rpx;
	text-align: center;
	margin: 70rpx;
}

.uiwu-list {
	padding: 0 24rpx;
	background-color: #fff;

	&-item {
		padding: 24rpx 0;
		border-bottom: 1rpx solid #E6E6E6;

		&:last-child {
			border-bottom: 0;
		}

		.name {
			font-size: 28rpx;
			font-weight: bold;
			color: #231815;
		}
	}
}
</style>
